#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include "protocol/routeinfo.pb.h"
#include "protocol/transport.pb.h"
#include "RouteInfoManager.h"
#include "model/ServerNode.h"
#include "codec/RequestDispatcher.h"
#include "network/reactor/Reactor.h"

// nameserver要处理心跳
// nameserver要处理路由信息查询请求 RouteQueryRequest
// nameserver要处理路由信息注册请求 RouteRegisterRequest
// nameserver要处理路由信息注销请求 RouteUnRegisterRequest
// nameserver扫描broker的存活状态不是走网络通信，而是遍历其管理的存活列表brokerLiveTable，这个table记录着上一次broker发送心跳的时间

namespace zeroplus::mq
{
    class NameSrvController
    {
    private:
        Reactor reactor_;
        RouteInfoManager routeInfoManager_;
        std::unique_ptr<ServerNode> server_;
        RequestDispatcher dispatcher_;

    public:
        NameSrvController(const std::string &uri);

        ~NameSrvController();

        // https://www.cnblogs.com/fengbohello/p/4743868.html
        bool Start();

        bool Stop();

        std::optional<transport::ReplyCommand> ProcessRequest(const transport::RequestCommand &requestCmd);

        transport::RegisterRouteInfoResponse ProcessRegisterRouteInfoRequest(const transport::RegisterRouteInfoRequest &request);

        transport::UnRegisterRouteInfoResponse ProcessUnRegisterRouteRequest(const transport::UnRegisterRouteInfoRequest &request);

        transport::QueryRouteInfoResponse ProcessQueryRouteInfoRequest(const transport::QueryRouteInfoRequest &request);

        transport::QueryBrokerClusterInfoResponse ProcessQueryBrokerClusterInfoRequest(const transport::QueryBrokerClusterInfoRequest &request);

        void ProcessHeartbeatMessage();
    };
}
